<?php
namespace app\admin\controller;

use app\admin\model\ArticleModel;
use think\Db;

class Articles extends Base
{
    // 文章列表
    public function index()
    {
        if(request()->isAjax()){
            $param = input('param.');
            $limit = $param['pageSize'];
            $offset = ($param['pageNumber'] - 1) * $limit;
            $where = [];
            if (!empty($param['searchText'])) {
                $where['title'] = ['like', '%' . $param['searchText'] . '%'];
				$where['cate_name'] = ['like', '%' . $param['searchText'] . '%'];
				$where['description'] = ['like', '%' . $param['searchText'] . '%'];
				$where['keywords'] = ['like', '%' . $param['searchText'] . '%'];
            }
            $article = new ArticleModel();
            $selectResult = $article->getArticlesByWhere($where, $offset, $limit);
            $return['total'] = $article->getAllArticles($where);  // 总数据
            $return['rows'] = $selectResult;
			return ['code'=>1,'msg'=>'表格加载成功','data'=>$return];
        }

        return $this->fetch();
    }

    // 添加文章
    public function articleAdd()
    {
        if(request()->isPost()){
            $param = input('post.');
            $param['add_time'] = date('Y-m-d H:i:s');
			$param['keywords'] = implode(",",$param['keywords']);
			$param['create_uid'] = session('id');
            $article = new ArticleModel();
            $flag = $article->addArticle($param);

            return json(msg($flag['code'], $flag['data'], $flag['msg']));
        }

        return $this->fetch();
    }

    public function articleEdit()
    {
        $article = new ArticleModel();
        if(request()->isPost()){
			$param = input('post.');
			$param['keywords'] = implode(",",$param['keywords']);
            $flag = $article->editArticle($param);
            return json(msg($flag['code'], $flag['data'], $flag['msg']));
        }
        $id = input('param.id');
        $this->assign([
            'article' => $article->getOneArticle($id)
        ]);
        return $this->fetch();
    }

    public function articleDel()
    {
        $id = input('param.id');
        $article = new ArticleModel();
        $flag = $article->delArticle($id);
        return json(msg($flag['code'], $flag['data'], $flag['msg']));
    }

    // 上传缩略图
    public function uploadImg()
    {
        if(request()->isAjax()){
            $file = request()->file('file');
            // 移动到框架应用根目录/public/uploads/ 目录下
            $info = $file->move(ROOT_PATH . 'public' . DS . 'upload');
            if($info){
                $src =  '/upload' . '/' . date('Ymd') . '/' . $info->getFilename();
                return json(msg(0, ['src' => $src], ''));
            }else{
                // 上传失败获取错误信息
                return json(msg(-1, '', $file->getError()));
            }
        }
    }

	public function cate() {
		if(request()->isAjax()){
            $cate = DB::name('articles_cate')->select();
            $cate = getTree(objToArray($cate), false);
            return json(msg(1, $cate, 'ok'));
        }
        return $this->fetch();
	}

	public function cateAdd() {
		$param = input('post.');
		$res = DB::name('articles_cate')->insert($param);
		if($res !== false){
			return ['code'=>1,'msg'=>'添加成功'];
		}else{
			return ['code'=>0,'msg'=>'添加失败'];
		}
	}

	public function cateEdit() {
		$param = input('post.');
		$res = DB::name('articles_cate')->where('id',$param['id'])->update($param);
		if($res !== false){
			return ['code'=>1,'msg'=>'编辑成功'];
		}else{
			return ['code'=>0,'msg'=>'编辑失败'];
		}
	}

	public function cateDel() {
		$param = input('post.');
		$res = DB::name('articles_cate')->where('id',$param['id'])->delete();
		if($res !== false){
			return ['code'=>1,'msg'=>'删除成功'];
		}else{
			return ['code'=>0,'msg'=>'删除失败'];
		}
	}
}
